package com.monkeyboy.mongo_demo;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.monkeyboy.util.MongoDBAuthUtil;
import org.bson.Document;
import org.bson.types.ObjectId;

import java.util.regex.Pattern;

/**
 * 查询文档
 *
 * @Author Gavin
 * @date 2021.01.11 09:54
 */
public class SelectDocument {
    public static void main(String[] args) {
        SelectDocument selectDocument = new SelectDocument();
        selectDocument.selectDocumentOrder();
    }

    /**
     * 排序处理
     * 根据age降序排序。1升序，-1降序
     * $sort:{age:-1}
     */
    public void selectDocumentOrder() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        //in第一个参数为键，后面的值作为值
        FindIterable<Document> documents = collection.find().sort(new Document("age",1));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags")+"\t"+doc.get("age"));
        }
    }

    /**
     * 逻辑运算符$and 与$or联合使用
     * 查询用户名为llq并且年纪大于20的或者用户名为test的文档
     */
    public void selectDocumentConditionByAndOr() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        //in第一个参数为键，后面的值作为值
        FindIterable<Document> documents = collection.find(Filters.or(Filters.and(Filters.eq("username", "llq"), Filters.gt("age", 20)), Filters.eq("username", "test")));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 逻辑运算符 $or
     */
    public void selectDocumentConditionByOr() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        //in第一个参数为键，后面的值作为值
        FindIterable<Document> documents = collection.find(Filters.or(Filters.eq("username", "llq"), Filters.eq("username", "test")));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 逻辑运算符 $and
     */
    public void selectDocumentConditionByAnd() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        //in第一个参数为键，后面的值作为值
        FindIterable<Document> documents = collection.find(Filters.and(Filters.eq("username", "Gavin"), Filters.eq("age", 10)));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 正则查询$regex
     */
    public void selectDocumentConditionByRegex() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        //in第一个参数为键，后面的值作为值
        FindIterable<Document> documents = collection.find(Filters.regex("username", Pattern.compile("^G.*n$")));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 查询多个条件 $nin
     * 表示不在这个集合中的文档
     */
    public void selectDocumentConditionByNin() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        //in第一个参数为键，后面的值作为值
        FindIterable<Document> documents = collection.find(Filters.nin("username", "Gavin", "llq"));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 查询多个条件 $in
     */
    public void selectDocumentConditionByIn() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        //in第一个参数为键，后面的值作为值
        FindIterable<Document> documents = collection.find(Filters.in("username", "Gavin", "llq"));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 根据value的类型来检索 $type
     */
    public void selectDocumentConditionByType() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        FindIterable<Document> documents = collection.find(Filters.type("age", "number"));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 查询多个文档
     * 查询年龄大于19岁的文档 $gt|$lt ....
     */
    public void selectManyDocumentByGt() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        FindIterable<Document> documents = collection.find(Filters.gt("age", 19));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 根据_id查询文档
     */
    public void selectDocumentById() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        FindIterable<Document> documents = collection.find(Filters.eq("_id", new ObjectId("5fface39cf39b578ad773d46")));
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }

    /**
     * 查询全部文档
     */
    public void selectDocumentAll() {
        MongoCollection<Document> collection = MongoDBAuthUtil.getCollection("test", "java_coll");
        //返回文档的一个迭代器
        FindIterable<Document> documents = collection.find();
        //通过游标来访问数据
        MongoCursor<Document> cursor = documents.iterator();
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            System.out.println(doc.get("username") + "\t" + doc.get("tags"));
        }
    }
}
